/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.server;

import java.util.List;

import org.lifebudget.app.client.mvc.model.Category;
import org.lifebudget.app.client.service.CategoryService;
import org.lifebudget.app.persistence.repository.CategoryRepository;
import org.lifebudget.app.persistence.repository.PaycheckCalculatorRepository;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The Class CategoryServiceImpl implements the {@link CategoryService} interface
 * and provides server-side functionality for GWT RPC.
 */
public class CategoryServiceImpl extends RemoteServiceServlet implements CategoryService {
	
	/** The category repo. */
	CategoryRepository categoryRepo;
	
	/** The paycheck repo. */
	PaycheckCalculatorRepository paycheckRepo;

	/**
	 * Instantiates a new category service impl.
	 */
	public CategoryServiceImpl() {
		super();
		categoryRepo = new CategoryRepository();
		paycheckRepo = new PaycheckCalculatorRepository();
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.CategoryService#getAllCategories(boolean)
	 */
	@Override
	public List<Category> getAllCategories(boolean includeDefault) {
		return categoryRepo.getAllCategories(includeDefault);
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.CategoryService#removeCategory(org.lifebudget.app.client.mvc.model.Category)
	 */
	@Override
	public List<Category> removeCategory(Category category) {
		// TODO may not need to "archive" every deleted category, just used
		// ones.
		System.out.println("removing category " + category.getName());
		categoryRepo.updateCategory(category);
		paycheckRepo.deleteCategoryAmountsForCategory(category.getId());
		return categoryRepo.getAllCategories(true);
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.CategoryService#updateCategories(java.util.List)
	 */
	@Override
	public List<Category> updateCategories(List<Category> categories) {

		for (Category category2 : categories) {
			System.out.println("UPDATING category " + category2.getName());
		}
		categoryRepo.updateCategories(categories);
		return categoryRepo.getAllCategories(false);

	}

}
